#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 3.3.22 5:20 PM
# @Author  : huanghe
# @Site    : 
# @File    : browser_manager.py
# @Software: PyCharm
from selenium import webdriver
from utils.path_manager import Path
from selenium.webdriver import ChromeOptions
import platform

DRIVER_PATH = Path().get_driver_path()
if platform.system().lower() == 'windows':
    CHROMEDRIVER_PATH = DRIVER_PATH + '/chromedriver.exe'
    FIREFOX_PATH = DRIVER_PATH + '/geckodriver.exe'
    IEDRIVER_PATH = DRIVER_PATH + '/IEDriverServer.exe'
    PHANTOMJSDRIVER_PATH = DRIVER_PATH + '/phantomjs.exe'
else:
    CHROMEDRIVER_PATH = DRIVER_PATH + '/chromedriver'
    FIREFOX_PATH = DRIVER_PATH + '/geckdriver'
    IEDRIVER_PATH = DRIVER_PATH + '/IEDriverServer'
    PHANTOMJSDRIVER_PATH = DRIVER_PATH + '/phantomjs'

TYPES = {'firefox': webdriver.Firefox, 'chrome': webdriver.Chrome, 'ie': webdriver.Ie, 'phantomjs': webdriver.PhantomJS}
EXECUTABLE_PATH = {'firefox': FIREFOX_PATH, 'chrome': CHROMEDRIVER_PATH, 'ie': IEDRIVER_PATH,
                   'phantomjs': PHANTOMJSDRIVER_PATH}


class Browser(object):
    def __init__(self, browser_type='chrome'):
        self._type = browser_type.lower()
        if self._type in TYPES:
            self.browser = TYPES[self._type]
        else:
            raise UnSupportBrowserTypeError('仅支持%s!' % ', '.join(TYPES.keys()))
        self.driver = None

    def get_driver(self, *args, **kwargs):
        self.driver = self.browser(executable_path=EXECUTABLE_PATH[self._type], *args, **kwargs)
        self.driver.maximize_window()
        return self.driver


class UnSupportBrowserTypeError(Exception):
    pass


if __name__ == '__main__':
    chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_argument('--headless')
    driver = Browser().get_driver(options=chrome_options)
    driver.get('https://ta-test.thinkingdata.cn')
    driver.close()
